
package com.zxq.ai.infrastructure.transaction;

import com.zxq.ai.application.helper.TransactionManagerProxy;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

@Component
public class TransactionManagerProxyImpl implements TransactionManagerProxy {

    @Resource
    private TransactionTemplate transactionTemplate;

    @Override
    public void execute(VoidSupplier voidSupplier) {
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
                voidSupplier.run();
            }
        });
    }

    @Override
    public <T> T execute(ResultSupplier<T> resultSupplier) {
        return transactionTemplate.execute(new TransactionCallback<T>() {
            @Override
            public T doInTransaction(TransactionStatus transactionStatus) {
                return resultSupplier.get();
            }
        });
    }
}
